package com.sxu.threadpool;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 阻塞队列
 */
public class MyBlockingQueue<T> {
    // 任务队列
    private Deque<T> queue = new ArrayDeque<T>();
    // 锁
    private ReentrantLock lock = new ReentrantLock();
    // 生产者条件变量
    private Condition fullWaitingSet = lock.newCondition();
    // 消费者条件变量
    private Condition emptyWaitingSet = lock.newCondition();
    // 容量上限
    private int capacity;

    // 构造方法
    public MyBlockingQueue(int capacity) {
        this.capacity = capacity;
    }

    // 对take进行改进，带超时的等待
    public T poll(long timeout, TimeUnit unit){
        // 加锁
        lock.lock();
        try {
            // 时间单位转换 转换为ns
            long nanos = unit.toNanos(timeout);
            while(queue.isEmpty()){
                try {
                    // 注意虚假唤醒问题，如果没等够指定时间，下次循环依旧会等待
                    // awaitNanos返回值为剩余时间 = 等待时间 - 已经等待的时间
                    if (nanos <= 0){
                        return null;
                    }
                    nanos = emptyWaitingSet.awaitNanos(nanos);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            T t = queue.removeFirst();
            // 队列不满了 需要唤醒
            fullWaitingSet.signal();
            return t;
        } finally {
            // 释放锁
            lock.unlock();
        }
    }

    // 阻塞获取
    public T take(){
        // 加锁
        lock.lock();
        try {
            while(queue.isEmpty()){
                try {
                    emptyWaitingSet.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            T t = queue.removeFirst();
            // 队列不满了 需要唤醒
            fullWaitingSet.signal();
            return t;
        } finally {
            // 释放锁
            lock.unlock();
        }
    }

    // 阻塞添加
    public void put(T task){
        // 加锁
        lock.lock();
        try {
            while (queue.size() == capacity){
                try {
                    System.out.println("等待加入任务队列" + task);
                    fullWaitingSet.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            queue.addLast(task);
            System.out.println("加入任务队列..." + task);
            // 队列不空 唤醒
            emptyWaitingSet.signal();
        }finally {
            // 释放锁
            lock.unlock();
        }
    }

    // 带超时时间的阻塞添加
    public boolean offer(T task, long timeout, TimeUnit unit){
        // 加锁
        lock.lock();
        try {
            long nanos = unit.toNanos(timeout);
            while (queue.size() == capacity){
                try {
                    System.out.println("等待加入任务队列" + task);
                    if(nanos <= 0){
                        return false;
                    }
                    fullWaitingSet.awaitNanos(nanos);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            queue.addLast(task);
            System.out.println("加入任务队列..." + task);
            // 队列不空 唤醒
            emptyWaitingSet.signal();
            return true;
        }finally {
            // 释放锁
            lock.unlock();
        }
    }

    // 实现tryPut方法
    public void tryPut(RejectPolicy<T> rejectPolicy,T task){
        // 加锁
        lock.lock();
        try {
            // 判断队列是否满了
            if(queue.size() == capacity){
                rejectPolicy.reject(this, task);
            } else {
                queue.addLast(task);
                System.out.println("加入任务队列..." + task);
            }

        } finally {
            // 解锁
            lock.unlock();
        }
    }


    // 获取大小
    public int size(){
        lock.lock();
        try {
            return queue.size();
        } finally {
            lock.unlock();
        }
    }
}
